package com.kgc.server.service.impl;

import com.kgc.server.dao.BuyCarDao;
import com.kgc.server.pojo.EasybuyCar;
import com.kgc.server.pojo.EasybuyProduct;
import com.kgc.server.service.EasybuyCarService;
import com.kgc.server.service.ProductService;
import com.kgc.server.util.ResponseMessage;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class EasybuyCarServiceImpl implements EasybuyCarService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private BuyCarDao buyCarDao;
    @Autowired
    private SolrClient solrClient;
    @Autowired
    private ProductService productService;


    @Override
    public ResponseMessage addProductToCar(EasybuyCar easybuyCar) {
        logger.info("EasybuyCarServiceImpl addProductToCar params: easybuyCar:"+easybuyCar);
        ResponseMessage msg = new ResponseMessage();
        ResponseMessage responseMessage = selectProductFromUserCar(easybuyCar.getUserId(), easybuyCar.getProductId());
        if(responseMessage.getData()==null){
            int i = buyCarDao.addProductToCar(easybuyCar);
            logger.info("EasybuyCarServiceImpl addProductToCar params: easybuyCar:"+easybuyCar+";result:"+i);
            msg.setMsg("添加成功！");
            msg.setCode("200");
            msg.setData(i);
        }else {
            EasybuyCar easybuyCar1 =(EasybuyCar) responseMessage.getData();
            logger.info("EasybuyCarServiceImpl addProductToCar params: easybuyCar:"+easybuyCar+"***easybuyCar1:"+easybuyCar1);
            Integer num = easybuyCar1.getProductNum();
            easybuyCar1.setProductNum(num+easybuyCar.getProductNum());
            msg = updateUserCar(easybuyCar1);
        }
        return msg;
    }

    @Override
    public ResponseMessage selectProductFromUserCar(String userId, String productId) {
        logger.info("EasybuyCarServiceImpl selectProductFromUserCar userId:"+userId+";productId:"+productId);
        ResponseMessage msg = new ResponseMessage();

        EasybuyCar easybuyCar = buyCarDao.selectProductFromUserCar(userId, productId);
        logger.info("EasybuyCarServiceImpl selectProductFromUserCar userId:"+userId+";productId:"+productId+";result:"+easybuyCar);
        msg.setData(easybuyCar);
        msg.setCode("200");
        msg.setMsg("查询成功！");
        return msg;
    }

    @Override
    public ResponseMessage updateUserCar(EasybuyCar easybuyCar) {
        ResponseMessage msg = new ResponseMessage();
        logger.info("EasybuyCarServiceImpl updateUserCar params: easybuyCar:"+easybuyCar);
        int i = buyCarDao.updateUserCar(easybuyCar);
        logger.info("EasybuyCarServiceImpl updateUserCar params: easybuyCar:"+easybuyCar+";result:"+i);
        msg.setMsg("修改成功！");
        msg.setCode("200");
        msg.setData(i);
        return msg;
    }

    @Override
    public ResponseMessage selectUserCar(String userId) {
        ResponseMessage msg = new ResponseMessage();
        logger.info("EasybuyCarServiceImpl selectUserCar param:"+userId);
        List<EasybuyCar> easybuyCars = buyCarDao.selectUserCar(userId);
        logger.info("EasybuyCarServiceImpl selectUserCar param:"+userId+";param:"+easybuyCars);
        msg.setData(easybuyCars);
        msg.setCode("200");
        msg.setMsg("查询成功！");
        return msg;
    }

    @Override
    public ResponseMessage deleteCarById(String id) {
        ResponseMessage msg = new ResponseMessage();
        logger.info("EasybuyCarServiceImpl deleteCarById param: id:"+id);
        int i = buyCarDao.deleteCarById(id);
        logger.info("EasybuyCarServiceImpl deleteCarById param: id:"+id+";result:"+i);
        msg.setMsg("删除成功！");
        msg.setData(i);
        msg.setCode("200");
        return msg;
    }
}
